<?php
/* 
 * Copyright (C) 2012 Leah Rowe <info@minifree.org>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/* Show server information */
	
include "oaprotected/common.php";

function getStats()
{
	$rxEmail = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(".
		"\.[a-z]{2,3})$/";
	$rxHostname = "/^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([".
		"A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$/";
	$rxIPv4Addr = "/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.".
		"){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/";
	
	// Output text (or error text)
	$output = "";
	$strError = "";
	
	// IP:Port variables
	$getIPAddr = "";
	$getPortNum = "";
	
	// check for existence of GET variable
	if (isset($_GET["ipaddr"])) $getIPAddr = clean($_GET["ipaddr"]);
	if (isset($_GET["portnum"])) $getPortNum = clean($_GET["portnum"]);
	
	// check for empty string
	if (strlen($getIPAddr) == 0) $strError .=
		"<li>IP Address not set in GET</li>";
	if (strlen($getPortNum) == 0) $strError .=
		"<li>Portnum not set in GET</li>";
	
	// if no errors so far, check the regular expression
	if (strlen($strError) == 0) 
	{
		if (!preg_match($rxIPv4Addr,$getIPAddr))
			$strError .= "<li>IPv4 Address does not conform to ".
			"regular expression.</li>";
		
		if (!is_numeric($getPortNum))
			$strError .= "<li>Portnum is not numerical.</li>";
		else if ((int)$getPortNum < 1 || (int)$getPortNum > 0xFFFE)
			$strError .= "<li>Not a valid port number.</li>";
	}
	
	// if no errors no far, check that this server exists in the database
	if (strlen($strError) == 0)
	{
		$sqlServers = inject("SELECT * FROM servers WHERE (ipaddr = ".
			"'$getIPAddr' AND portnum = '$getPortNum')");
		$sqlServersNrows = mysql_numrows($sqlServers);
		
		// if server does not exist, return with error
		if ($sqlServersNrows == 0)
			$strError .= "<li>Server $getIPAddr:$getPortNum does ".
			"not exist in the database.</li>";
			
		// if server exists, check whether it is listed as online
		else
		{
			$sqlServersServerDown = mysql_result($sqlServers, 0,
				"serverdown");
			
			if ($sqlServersServerDown == "1")
				$strError .= "<li>That server is down at the ".
				"moment, according to the database.</li>";
		}
	}
	
	// if there are input errors, return with an error list
	if (strlen($strError) > 0)
		return "<h1>There were errors</h1><ul>$strError</ul>";
		
	/* --- Error checking done, input was valid. ---- */

	// find that server on dpmaster
	$sqlQStat = inject("SELECT * FROM qstat");
	$sqlQStatNrows = mysql_numrows($sqlQStat);
	
	for ($i = 0; $i < $sqlQStatNrows; $i++) {
		$sqlQStatListURL = mysql_result($sqlQStat, $i, "listurl");
		$content = curlGetPage($sqlQStatListURL . 
			(strpos($sqlQStatListURL,"?")!==false?"&":"?") .
			"server=$getIPAddr:$getPortNum&xml=1");

		$qstat = new SimpleXMLElement($content);
		
		$server_status = $qstat->server[0]['status'];
		if ($server_status != "UP") {
			continue;
		}
		
		// get name of server, set as page title <h1>
		foreach($qstat->server[0]->rules->rule as $rule) {
			if ($rule['name'] == "sv_hostname")
				$output .= "<h1>$rule</h1>";
		}
		// get player information
		$output .= "<table class='playertable'>";
		$output .= "<tr><th>Player</th><th>Score</th><th>Ping</th>".
			"</tr>";
		foreach($qstat->server[0]->players->player as $player) {
			$output .= "<tr><td>".$player->name."</td><td>".
				$player->score."</td><td>".$player->ping.
				"</td></tr>";
		}
		$output .= "</table>";
		// get other server statistics
		$output .= "<h1>Server Statistics</h1>";
		$output .= "<table class='ruletable'>";
		$output .= "<tr><th>Hostname</th><td>".
			$qstat->server[0]->hostname."</td></tr>";
		$output .= "<tr><th>Name</th><td>".
			$qstat->server[0]->name."</td></tr>";
		$output .= "<tr><th>Gametype</th><td>".
			$qstat->server[0]->gametype."</td></tr>";
		$output .= "<tr><th>Map</th><td>".
			$qstat->server[0]->map."</td></tr>";
		$output .= "<tr><th>Players</th><td>".
			$qstat->server[0]->numplayers."</td></tr>";
		$output .= "<tr><th>Maxplayers</th><td>".
			$qstat->server[0]->maxplayers."</td></tr>";
		$output .= "<tr><th>Ping</th><td>".
			$qstat->server[0]->ping."</td></tr>";
		$output .= "<tr><th>Retries</th><td>".
			$qstat->server[0]->retries."</td></tr>";
		$output .= "</table>";
		// get output for server rules
		$output .= "<h1>Server Rules</h1>";
		$output .= "<table class='ruletable'>";
		foreach($qstat->server[0]->rules->rule as $rule) {
			$output .= "<tr><th>".$rule['name']."</th>".
				"<td>".$rule."</td></tr>";
		}
		$output .= "</table>";
	}
	
	return "$output";
}
?>
